home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / io / printstr.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  6.6 KB  |  299 lines

  1. /*
  2.  * @(#)PrintStream.java    1.22 95/08/12 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. /**
  23.  * This class implements an output stream that has
  24.  * additional methods for printing. You can specify
  25.  * that the stream should be flushed every time a
  26.  * newline character is written.<p>
  27.  *
  28.  * <em>The top byte of 16 bit characters is discarded.</em><p>
  29.  * Example:
  30.  * <pre>
  31.  *    System.out.println("Hello world!");
  32.  *    System.out.print("x = ");
  33.  *    System.out.println(x);
  34.  *    System.out.println("y = " + y);
  35.  * </pre>
  36.  *
  37.  * @version     1.22, 08/12/95
  38.  * @author    Arthur van Hoff
  39.  */
  40. public
  41. class PrintStream extends FilterOutputStream {
  42.     private boolean autoflush;
  43.     private boolean trouble;
  44.  
  45.     /**
  46.      * Creates a new PrintStream.
  47.      * @param out    the output stream
  48.      */
  49.     public PrintStream(OutputStream out) {
  50.     this(out, false);
  51.     trouble = false;
  52.     }
  53.  
  54.     /**
  55.      * Creates a new PrintStream, with auto flushing.
  56.      * @param out    the output stream
  57.      * @param autoflush if true the stream automatically flushes
  58.      *        its output when a newline character is printed
  59.      */
  60.     public PrintStream(OutputStream out, boolean autoflush) {
  61.     super(out);
  62.     this.autoflush = autoflush;
  63.     trouble = false;
  64.     }
  65.  
  66.     /**
  67.      * Writes a byte. This method will block until the byte is actually
  68.      * written.
  69.      * @param b the byte
  70.      * @exception IOException If an I/O error has occurred.
  71.      */
  72.     public void write(int b) {
  73.         try {
  74.         out.write(b);
  75.         if (autoflush && (b == '\n')) {
  76.             out.flush();
  77.         }
  78.       } catch (InterruptedIOException ex) {
  79.         // We've been interrupted.  Make sure we're still interrupted.
  80.         Thread.currentThread().interrupt();
  81.     } catch (IOException ex) {
  82.         trouble = true;
  83.     }
  84.     }
  85.  
  86.     /**
  87.      * Writes a sub array of bytes. 
  88.      * @param b    the data to be written
  89.      * @param off    the start offset in the data
  90.      * @param len    the number of bytes that are written
  91.      * @exception IOException If an I/O error has occurred.
  92.      */
  93.     public void write(byte b[], int off, int len) {
  94.     try {
  95.         out.write(b, off, len);
  96.         if (autoflush) {
  97.             out.flush();
  98.         }
  99.       } catch (InterruptedIOException ex) {
  100.         // We've been interrupted.  Make sure we're still interrupted.
  101.         Thread.currentThread().interrupt();
  102.     } catch (IOException ex) {
  103.         trouble = true;
  104.     }
  105.     }
  106.  
  107.     /**
  108.      * Flushes the stream. This will write any buffered
  109.      * output bytes.
  110.      */
  111.     public void flush() {
  112.     try {
  113.         super.flush();
  114.     } catch (IOException ex) {
  115.         trouble = true;
  116.     }
  117.     }
  118.     /**
  119.      * Closes the stream.
  120.      */
  121.     public void close() {
  122.     try {
  123.         super.close();
  124.     } catch (IOException ex) {
  125.         trouble = true;
  126.     }
  127.     }
  128.  
  129.     
  130.     /**
  131.      * Prints an object.
  132.      * @param obj the object to be printed
  133.      */
  134.     public void print(Object obj) {
  135.     print(String.valueOf(obj));
  136.     }
  137.  
  138.     /**
  139.      * Prints a String.
  140.      * @param s the String to be printed
  141.      */
  142.     synchronized public void print(String s) {
  143.     if (s == null) {
  144.         s = "null";
  145.     }
  146.  
  147.     int len = s.length();
  148.     for (int i = 0 ; i < len ; i++) {
  149.         write(s.charAt(i));
  150.     }
  151.     }
  152.  
  153.     /**
  154.      * Prints an array of characters.
  155.      * @param s the array of chars
  156.      */
  157.     synchronized public void print(char s[]) {
  158.     for (int i = 0 ; i < s.length ; i++) {
  159.         write(s[i]);
  160.     }
  161.     }
  162.  
  163.     /**
  164.      * Prints an character.
  165.      * @param c the character to be printed
  166.      */
  167.     public void print(char c) {
  168.     print(String.valueOf(c));
  169.     }
  170.  
  171.     /**
  172.      * Prints an integer.
  173.      * @param i the integer to be printed
  174.      */
  175.     public void print(int i) {
  176.     print(String.valueOf(i));
  177.     }
  178.  
  179.     /**
  180.      * Prints a long.
  181.      * @param l the long
  182.      */
  183.     public void print(long l) {
  184.     print(String.valueOf(l));
  185.     }
  186.  
  187.     /**
  188.      * Prints a float.
  189.      * @param f the float to be printed
  190.      */
  191.     public void print(float f) {
  192.     print(String.valueOf(f));
  193.     }
  194.  
  195.     /**
  196.      * Prints a double.
  197.      * @param d the double to be printed
  198.      */
  199.     public void print(double d) {
  200.     print(String.valueOf(d));
  201.     }
  202.  
  203.     /**
  204.      * Prints a boolean.
  205.      * @param b the boolean to be printed
  206.      */
  207.     public void print(boolean b) {
  208.     print(b ? "true" : "false");
  209.     }
  210.     
  211.     /**
  212.      * Prints a newline.
  213.      */
  214.     public void println() {
  215.     write('\n');
  216.     }
  217.     
  218.     /**
  219.      * Prints an object followed by a newline.
  220.      * @param obj the object to be printed
  221.      */
  222.     synchronized public void println(Object obj) {
  223.     print(obj);
  224.     write('\n');
  225.     }
  226.  
  227.     /**
  228.      * Prints a string followed by a newline.
  229.      * @param s the String to be printed
  230.      */
  231.     synchronized public void println(String s) {
  232.     print(s);
  233.     write('\n');
  234.     }
  235.     
  236.     /**
  237.      * Prints an array of characters followed by a newline.
  238.      * @param s the array of characters to be printed
  239.      */
  240.     synchronized public void println(char s[]) {
  241.     print(s);
  242.     write('\n');
  243.     }
  244.     
  245.     /**
  246.      * Prints a character followed by a newline.
  247.      * @param c the character to be printed
  248.      */
  249.     synchronized public void println(char c) {
  250.     print(c);
  251.     write('\n');
  252.     }
  253.  
  254.     /**
  255.      * Prints an integer followed by a newline.
  256.      * @param i the integer to be printed
  257.      */
  258.     synchronized public void println(int i) {
  259.     print(i);
  260.     write('\n');
  261.     }
  262.  
  263.     /**
  264.      * Prints a long followed by a newline.
  265.      * @param l the long to be printed
  266.      */
  267.     synchronized public void println(long l) {
  268.     print(l);
  269.     write('\n');
  270.     }
  271.  
  272.     /**
  273.      * Prints a float followed by a newline.
  274.      * @param f the float to be printed
  275.      */
  276.     synchronized public void println(float f) {
  277.     print(f);
  278.     write('\n');
  279.     }
  280.  
  281.     /**
  282.      * Prints a double followed by a newline.
  283.      * @param d the double to be printed
  284.      */
  285.     synchronized public void println(double d) {
  286.     print(d);
  287.     write('\n');
  288.     }
  289.  
  290.     /**
  291.      * Prints a boolean followed by a newline.
  292.      * @param b the boolean to be printed
  293.      */
  294.     synchronized public void println(boolean b) {
  295.     print(b);
  296.     write('\n');
  297.     }
  298. }
  299.